package high.concurrent.controller;

import high.concurrent.request.AddCartRequest;
import high.concurrent.service.CartService;
import high.concurrent.utils.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "购物车模块")
@RequestMapping("/api/cart/v1/")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 添加购物车
     * @param addCartRequest   购物车对象
     * @return
     */
    @ApiOperation("添加购物车商品")
    @PostMapping("addCart")
    public JsonData addCart(@ApiParam(value = "添加购物车信息", required = true) @RequestBody AddCartRequest addCartRequest){

        JsonData jsonData = cartService.addCart(addCartRequest);
        return jsonData;
    }


    /**
     * 清空购物车
     * @return
     */
    @ApiOperation("清空购物车商品")
    @PostMapping("deleteCart")
    public void deleteCart(){
        cartService.deleteCart();
    }


    /**
     * 查询购物车
     * @return
     */
    @ApiOperation("查询购物车商品")
    @GetMapping("selectCart")
    public JsonData selectCart(){
        JsonData jsonData = cartService.selectCart();
        return jsonData;
    }


    /**
     * 删除某个购物项
     * @param productId 商品id
     * @return
     */
    @ApiOperation("删除某个购物项")
    @PostMapping("deleteCartItem")
    public void deleteCartItem(@ApiParam(value = "商品id", required = true) @RequestParam(value = "product_id") Long productId){
        cartService.deleteCartItem(productId);
    }


    /**
     * 修改某个购物项数量
     * @param  addCartRequest   商品id , 数量
     * @return
     */
    @ApiOperation("修改某个购物项数量")
    @PostMapping("updateCartItemCount")
    public void updateCartItemCount(@ApiParam(value = "添加购物车信息", required = true) @RequestBody AddCartRequest addCartRequest){
        cartService.updateCartItemCount(addCartRequest);
    }


    /**
     * 获取购物车商品项的最新价格，并删除对应购物车商品
     * @param productIdList
     * @return
     */
    @ApiOperation(value = "获取购物车商品项的最新价格，并删除对应购物车商品")
    @PostMapping("getCartItemLatestPrice")
    JsonData getCartItemLatestPrice(@ApiParam(value = "购物项id的集合") @RequestBody List<Long> productIdList){

        JsonData jsonData = cartService.getCartItemLatestPrice(productIdList);
        return jsonData;
    }
}
